<?php
session_start();
/**
 * JS_API支付demo
 * ====================================================
 * 在微信浏览器里面打开H5网页中执行JS调起支付。接口输入输出数据格式为JSON。
 * 成功调起支付需要三个步骤：
 * 步骤1：网页授权获取用户openid
 * 步骤2：使用统一支付接口，获取prepay_id
 * 步骤3：使用jsapi调起支付
*/
	$appid = 'wx1a7fc6711244e4f4';
	$appsecret = '860f2c3adfcfb3743fe538f6281ddbfa';
	function _curl($url) {
		
		$ch = curl_init();
		
		curl_setopt($ch, CURLOPT_TIMEOUT, 30);
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
		curl_setopt($ch, CURLOPT_HEADER, FALSE);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
		$res = curl_exec($ch);
		curl_close($ch);
		$data = json_decode($res, true);
		return $data;
	}
	//step 1 get code
	
	if (!isset($_GET['code']))
	{
		//触发微信返回code码
		$retUrl =  'http://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];
		$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=$appid&redirect_uri=$retUrl&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
		Header("Location: $url"); 
	} else {
		$code = $_GET['code'];
		$url ="https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
		$data = _curl($url);
		//var_dump($data);
		$accessToken = $data['access_token'];
		$_SESSION['accessToken'] = $accessToken;
		header('Location:addr.php');
	}
	
	
	
	
	

?>
